﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;

namespace WebC
{
    public partial class base_companylist : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            topage.iRecordNum = iPageRowNum;
            btnDele.Attributes["onclick"] = "return deledata()";
            btnEdit.Attributes["onclick"] = "return editdata('company.aspx')";
            btnAdd.Attributes["onclick"] = "return goaddurl('company.aspx')";
            btnBrand.Attributes["onclick"] = "return editdata()";
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        int iPageId
        {
            get
            {
                return Convert.ToInt32(Request["page"]) == 0 ? 1 : Convert.ToInt32(Request["page"]);
            }
        }

        public bool bIsEdit = false;
        public bool bIsView = false;

        public void loadInfo()
        {
            BaseInfo baseinfo = null;
            string sFullName = "";
            string sWhere = @"";
            ddlColName.SelectedValue = Convert.ToString(Request["colname"]);
            ddlType.SelectedValue = Convert.ToString(Request["type"]);
            txtWord.Text = Convert.ToString(Request["word"]);
            ddlCompanyType.SelectedValue = Convert.ToString(Request["ctype"]);
            int iType = Convert.ToInt32(ddlCompanyType.SelectedValue);
            if (iType > 0)
            {
                sWhere += string.Format(" and companytype={0}", iType - 1);
            }
            if (txtWord.Text != "")
            {
                switch (ddlColName.SelectedValue)
                {
                    default:
                        if (ddlType.SelectedValue == "like")
                        {
                            sWhere = " and " + ddlColName.SelectedValue + " " + ddlType.SelectedValue + "'%" + txtWord.Text + "%'";
                        }
                        else
                        {
                            sWhere = " and " + ddlColName.SelectedValue + " " + ddlType.SelectedValue + "'" + txtWord.Text + "'";
                        }
                        break;
                }
            }
            try
            {
                baseinfo = new BaseInfo();
                if (!checkModelRightById(521, ref sFullName, (clsBaseLib)baseinfo))
                {
                    errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sFullName));
                    return;
                }
                sModelFullName = sFullName;
                bIsView = checkModelRightById(522, ref sFullName, (clsBaseLib)baseinfo);
                btnAdd.Visible = checkModelRightById(523, ref sFullName, (clsBaseLib)baseinfo);
                btnDele.Visible = checkModelRightById(524, ref sFullName, (clsBaseLib)baseinfo);
                bIsEdit = checkModelRightById(525, ref sFullName, (clsBaseLib)baseinfo);
                btnEdit.Visible = bIsEdit;
                btnBrand.Visible = checkModelRightById(526, ref sFullName, (clsBaseLib)baseinfo);
                rpttblInfo.DataSource = baseinfo.getCompanyList(iPageId, sWhere);
                rpttblInfo.DataBind();
                int iRecourdCount = Request["record"] != null ? Convert.ToInt32(Request["record"]) : baseinfo.getCompanyCount(sWhere);
                topage.iRecordCount = iRecourdCount;
                topage.sPageUrl = string.Format("companylist.aspx?colname={0}&type={1}&word={2}&record={3}&ctype={4}", HttpUtility.UrlEncodeUnicode(ddlColName.SelectedValue)
                    , HttpUtility.UrlEncodeUnicode(ddlType.SelectedValue), HttpUtility.UrlEncodeUnicode(txtWord.Text), iRecourdCount, iType);
            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("companylist.aspx?colname={0}&type={1}&word={2}&ctype={3}", ddlColName.SelectedValue
                    , ddlType.SelectedValue, txtWord.Text, ddlCompanyType.SelectedValue));
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("company.aspx?id={0}", Convert.ToInt32(Request["id"])));
        }

        protected void btnDele_Click(object sender, EventArgs e)
        {
            BaseInfo baseinfo = null;
            bool bResult = false;
            if (!checkIdsData(hdids.Value))
            {
                return;
            }
            try
            {
                baseinfo = new BaseInfo();
                bResult = baseinfo.deleteCompanyByIds(hdids.Value);
            }
            catch (Exception ex)
            {
                errorUrl(ex.ToString());
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
            if (bResult)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "delesuc", "<script>alert('删除成功！');backreflesh()</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "delesuc", "<script>alert('删除失败！');</script>");
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("company.aspx?id={0}", hdids.Value));
        }
        protected void btnBrand_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("companybrand.aspx?id={0}", hdids.Value));
        }
}
}